const express = require('express');
const router = express.Router();
const jwt = require('jsonwebtoken');
const passport = require('passport');

const Catagory = require('../../models/Catagory');


router.get('/', (req,res)=>{
	res.json([
		{
			描述:'请求 blog 详情的时候，自动更新 Catagory 、blog 数据库的view、view_num字段，新建博客自动更新 Catagory 的blog_num数量，更新blog文章的时候，自动更新Catagory的数量，前端不需要在请求blog的时候处理Catagory数据'
		},
		{
			title:'Catagory 列表',
			api:'api/catagoy/list',
			post:'get',
			params:{
				page:'第几页',
				pageSize:'一页显示几条数据',
				title:'标题(模糊查询)'
			}
		},
		{
			title:'catagoy 新增',
			api:'api/catagoy/add',
			post:'post',
			params:{
				status:'状态,必填',
				blogPic:'封面图片',
				title:'标题,必填',
				relatedBlogs:'数组，最大支持3个, 格式[{id:2,name:2222,...},....]',
				conent:'文章内容,必填',
				country:'{id:1,name:q111,...},必填',
				city:'{id:1,name:q111,...},必填',
				authour:'{id:1,name:q111,...}',
				catagory:'{id:1,name:q111,...},必填',
				location:'{id:1,name:q111,...},必填',
				blogLanguage:'文章语言,必填'
			},
			Header: '头部验证 Header：{Authorization:token字段}'
		},
		{
			title:'catagoy 删除',
			api:'api/catagoy/delete/:id',
			post:'delete',
			Header: '头部验证 Header：{Authorization:token字段}'
		},{
			title:'catagoy 编辑',
			api:'api/catagoy/edit/:id',
			post:'put',
			params:{
				title:'标题,必填',
			},
			Header: '头部验证 Header：{Authorization:token字段}'
		}
	])
})
router.post('/add', passport.authenticate('jwt',{session:false}),(req, res) => {
	const params ={}

	if(req.body.title) params.title = req.body.title
	new Catagory(params).save().then(data=>{
		res.json({
			data:data,
			status:2
		})
	}).catch(err=>{
		res.status(400).json({
			msg:err,
			status:0
		})
	})
})

router.put('/edit/:id',passport.authenticate('jwt',{session:false}), (req, res) => {
	const params ={}
	if(req.body.title) params.title = req.body.title
	Catagory.findOneAndUpdate(
		{_id:req.params.id},
		{$set:params},
		{new:true}
	).then(profile => res.json({
		data:profile,
		status:2
	})).catch(err=>res.status(404).json({
		msg:err,
		status:0
	}))
})

router.delete('/delete/:id',passport.authenticate('jwt',{session:false}), (req, res) => {
	const params ={}
	if(req.body.title) params.title = req.body.title
	Catagory.findOne({_id:req.params.id}).then(profile=>{
		if(profile && profile.blog_num == 0){
			Catagory.findOneAndRemove({_id:req.params.id}).then(profile => res.json({
				data:profile,
				status:2
			})).catch(err=>res.status(404).json({
				msg:err,
				status:0
			}))
		} else {
			res.status(400).json({
				msg:'Catagory 存在博客',
				status:0
			})
		}

	})

})
router.get('/list/',(req, res) => {
	let count =0;
	let query = {};
	if (req.query.title) query.title = {$regex:req.query.title};
	let page = parseInt(req.query.page) || 1;
	let rows = req.query.pageSize || 10;
	let skip=0;
	let limit = 0;
	if(page <= 1){
		skip =0;
	} else {
		skip = rows*page - rows
	}
	Catagory.find(query).then(all=>{
		count = all.length
	}).then(() => {
		//总页数 大于当前页数
		if(Math.ceil(count/rows) >= page){
			if(Math.ceil(count/rows) == page){
				limit = count
			} else {
				limit = parseInt(rows)
			}
		} else {
			return res.json({
				data:[],
				status:1,
				msg:'没有数据'
			});
		}
		Catagory.find(query).skip(skip).limit(limit).sort({date:-1}).then(pageData => {
			if (!pageData){
				return res.json({
					data:[],
					count:0,
					page:page,
					status:1,
				})
			}
			res.json({
				data:pageData,
				count,
				page:page,
				status:2,
			})
		}).catch(err=>console.log(err))
	}).catch(err=>console.log(err))
})


module.exports = router;
